package com.quectel.business.common.workorder;

import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.constant.core.workorder.WorkOrderConstants;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.workorder.dto.WorkOrderDto;
import com.quectel.core.module.workorder.dto.WorkOrderLogDto;
import com.quectel.core.module.workorder.service.WorkOrderLogService;
import com.quectel.core.module.workorder.service.WorkOrderService;
import com.quectel.sys.module.sys.dto.SysCodeDto;
import com.quectel.sys.module.sys.dto.SysUserDto;
import com.quectel.sys.module.sys.service.SysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 工单的详情
 *
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/01/19 09:57
 */
@Component
public class WorkOrderBusiness {
    @DubboReference
    private WorkOrderService workOrderService;
    @DubboReference
    private WorkOrderLogService workOrderLogService;
    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private SysUserService sysUserService;
    @DubboReference
    private DeptPersonService deptPersonService;

    public WorkOrderDto selectById(@PathVariable("id") Long id) {

        WorkOrderDto workOrderDto = workOrderService.selectById(id);
        workOrderDto.setStatusName(SysCodeUtils.easyToGetName(SysCodeKeyConstants.WORK_ORDER_STATUS, String.valueOf(workOrderDto.getStatus())));

        if (workOrderDto == null) {
            return null;
        }
        if (WorkOrderConstants.ORDER_SOURCE_ADMIN.equals(workOrderDto.getOrderSource())) {
            SysUserDto sysUserDto = sysUserService.selectById(workOrderDto.getSourceId());
            if (sysUserDto != null) {
//                workOrderDto.setOrderSourceUsername(DesensitizedUtil.chineseName(sysUserDto.getUsername()));
//                workOrderDto.setOrderSourceUserMobile(DesensitizedUtil.mobilePhone(sysUserDto.getMobile()));
                workOrderDto.setOrderSourceUsername(sysUserDto.getUsername());
                workOrderDto.setOrderSourceUserMobile(sysUserDto.getMobile());
            }
        } else if (WorkOrderConstants.ORDER_SOURCE_HOUSEHOLD.equals(workOrderDto.getOrderSource())) {
            HouseholdDto householdDto = householdService.selectById(workOrderDto.getSourceId());
            if (householdDto != null) {
//                workOrderDto.setOrderSourceUsername(DesensitizedUtil.chineseName(householdDto.getName()));
//                workOrderDto.setOrderSourceUserMobile(DesensitizedUtil.mobilePhone(householdDto.getMobile()));
                workOrderDto.setOrderSourceUsername(householdDto.getName());
                workOrderDto.setOrderSourceUserMobile(householdDto.getMobile());
            }
        } else if (WorkOrderConstants.ORDER_SOURCE_DEPT_PERSON.equals(workOrderDto.getOrderSource())) {
            DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(workOrderDto.getSourceId());
            if (deptPersonDto != null) {
//                workOrderDto.setOrderSourceUsername(DesensitizedUtil.chineseName(deptPersonDto.getName()));
//                workOrderDto.setOrderSourceUserMobile(DesensitizedUtil.mobilePhone(deptPersonDto.getMobile()));
                workOrderDto.setOrderSourceUsername(deptPersonDto.getName());
                workOrderDto.setOrderSourceUserMobile(deptPersonDto.getMobile());
            }
        }

        if (workOrderDto.getHandlerId() != null) {
            DeptPersonDto personDto = deptPersonService.selectCacheById(workOrderDto.getHandlerId());
            if (personDto != null) {
                workOrderDto.setHandlerUsername(personDto.getName());
            }
        }

        List<WorkOrderLogDto> logs = workOrderLogService.selectByWorkOrderId(id);
        if (logs != null) {
            for (WorkOrderLogDto log : logs) {
                if (WorkOrderConstants.ORDER_HANDLER_TYPE_ADMIN.equals(log.getHandlerType())) {
                    SysUserDto logUser = sysUserService.selectCacheById(log.getHandlerId());
                    if (logUser != null) {
                        log.setHandlerUsername(logUser.getUsername());
                        log.setHandlerMobile(logUser.getMobile());
                    }
                } else if (WorkOrderConstants.ORDER_HANDLER_TYPE_DEPT_PERSON.equals(log.getHandlerType())) {
                    DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(log.getHandlerId());
                    if (deptPersonDto != null) {
                        log.setHandlerUsername(deptPersonDto.getName());
                        log.setHandlerMobile(deptPersonDto.getMobile());
                    }
                }
                log.setOrderStatusName(SysCodeUtils.easyToGetName(SysCodeKeyConstants.WORK_ORDER_STATUS, String.valueOf(log.getOrderStatus())));
            }
            Collections.sort(logs);
            workOrderDto.setWorkOrderLogDtoList(logs);
        }

        return workOrderDto;
    }

    /**
     * 得到工单分类
     *
     * @param workOrderDto
     * @return
     */
    public String[] getCategoryAndType(WorkOrderDto workOrderDto) {
        String categoryName = "";
        String typeName = "";
        List<SysCodeDto> workOrderCategory = SysCodeUtils.easyToGetByMark(SysCodeKeyConstants.WORK_ORDER_CATEGORY);
        if (workOrderCategory != null) {
            for (SysCodeDto sysCodeDto : workOrderCategory) {
                if (Objects.equals(workOrderDto.getCategory() + "", sysCodeDto.getValue())) {
                    categoryName = sysCodeDto.getName();
                    List<SysCodeDto> children = sysCodeDto.getChildren();
                    if (children != null) {
                        for (SysCodeDto child : children) {
                            if (Objects.equals(child.getValue(), workOrderDto.getType() + "")) {
                                typeName = child.getName();
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
        return new String[]{categoryName, typeName};
    }

}
